package com.bytedance.scenedemo.animatiom;

import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.PushOptions;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.animatiom.sipnner.Popup;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JiangQi on 8/15/18.
 */
public class AnimationResDemoScene extends GroupScene {
    private ComponentContainer componentContainer;
    private int width;
    /**
     * 进场动画数据集合
     */
    private List<String> containerCenterData = new ArrayList<>();
    /**
     * 出场动画数据集合
     */
    private List<String> containerExitData = new ArrayList<>();
    private Text textCenterName;
    private Text textExitName;
    private boolean isPushToLeft = false;


    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_animation_res_demo_scene, container, false);
        initEnterAnimationData();
        return componentContainer;
    }

    private void initEnterAnimationData() {
        initData();
        width = getAbility().getWindow().getLayoutConfig().get().width;
        textCenterName = (Text) componentContainer.findComponentById(ResourceTable.Id_txt_center_animation_name);
        textExitName = (Text) componentContainer.findComponentById(ResourceTable.Id_text_exit_name);
        Button btn = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_xml_animated_transitions);
        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                NavigationSceneGetter.requireNavigationScene(AnimationResDemoScene.this).push(EmptyScene.class, null
                        , new PushOptions.Builder().setAnimation(getAbility(), getPushEnter(), getPushExit(), getPopEnter(), getPopExit(), isPushToLeft, isPushToRight).build());
            }
        });
        DirectionalLayout directionalLayoutCenter = (DirectionalLayout) componentContainer.findComponentById(ResourceTable.Id_directionalLayout_center);
        directionalLayoutCenter.setClickedListener(component -> new Popup.Builder(getAbility())
                .hasShadowBg(false)
                .isDestroyOnDismiss(true) // 对于只使用一次的弹窗，推荐设置这个
                .atView(component)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(containerCenterData,
                        (position, text) -> textCenterName.setText(text)).show());
        DirectionalLayout directionalLayoutExit = (DirectionalLayout) componentContainer.findComponentById(ResourceTable.Id_directionalLayout_exit);
        directionalLayoutExit.setClickedListener(component -> new Popup.Builder(getAbility())
                .hasShadowBg(false)
                .isDestroyOnDismiss(true) // 对于只使用一次的弹窗，推荐设置这个
                .atView(component)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(containerExitData,
                        (position, text) -> textExitName.setText(text)).show());
        textCenterName.setText(containerCenterData.get(0));
        textExitName.setText(containerExitData.get(0));
    }

    private void initData() {
        containerCenterData.add("slide_in_from_right");
        containerCenterData.add("abc_fade_in");
        containerCenterData.add("fade_in");
        containerCenterData.add("slide_in_left");

        containerExitData.add("slide_out_to_left");
        containerExitData.add("abc_fade_out");
        containerExitData.add("fade_out");
        containerExitData.add("slide_out_right");
    }

    private boolean isPushToRight = false;

    // toView
    private AnimatorProperty pushEnter1() {
        isPushToLeft = true;
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(width)
                .moveToX(0)
                .setCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    // from
    private Animator pushExit1() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(0)
                .moveToX(-width)
                .setCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    // toView
    private AnimatorProperty popEnter1() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(-width)
                .moveToX(0)
                .setCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    // fromView
    private Animator popExit1() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(0)
                .moveToX(width)
                .setCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }


    private AnimatorProperty pushEnter2() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(0f)
                .alpha(1.0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }


    private Animator pushExit2() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(1.0f)
                .alpha(0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }


    private AnimatorProperty popEnter2() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(0f)
                .alpha(1.0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator popExit2() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(1.0f)
                .alpha(0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    private AnimatorProperty pushEnter3() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(0.0f)
                .alpha(1.0f)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator pushExit3() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(1.0f)
                .alpha(0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    private AnimatorProperty popEnter3() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(0f)
                .alpha(1.0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator popExit3() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .alphaFrom(1.0f)
                .alpha(0f)
                .setCurveType(Animator.CurveType.DECELERATE)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator pushEnter4() {
        isPushToRight = true;
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(-width)
                .moveToX(0)
                .alphaFrom(0.0f)
                .alpha(1.0f)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator pushExit4() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(0)
                .moveToX(width)
                .alphaFrom(1.0f)
                .alpha(0f)
                .setDuration(400);
        return animatorProperty;
    }

    private AnimatorProperty popEnter4() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(width)
                .moveToX(0)
                .alphaFrom(0f)
                .alpha(1.0f)
                .setDuration(400);
        return animatorProperty;
    }

    private Animator popExit4() {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty
                .moveFromX(0)
                .moveToX(-width)
                .alphaFrom(1.0f)
                .alpha(0f)
                .setDuration(400);
        return animatorProperty;
    }


    private Animator getPushEnter() {
        isPushToLeft = false;
        isPushToRight = false;
        int enter = containerCenterData.indexOf(textCenterName.getText());
        System.out.println("================getPushEnter》》》" + enter);
        switch (enter) {
            case 0:
                return pushEnter1();
            case 1:
                return pushEnter2();
            case 2:
                return pushEnter3();
            case 3:
                return pushEnter4();
            default:
                return null;
        }
    }

    private Animator getPushExit() {
        int enter = containerExitData.indexOf(textExitName.getText());
        System.out.println("================getPushExit》》》" + enter);
        switch (enter) {
            case 0:
                return pushExit1();
            case 1:
                return pushExit2();
            case 2:
                return pushExit3();
            case 3:
                return pushExit4();
            default:
                return null;
        }
    }

    private Animator getPopEnter() {
        int enter = containerCenterData.indexOf(textCenterName.getText());
        System.out.println("================getPopEnter》》》" + enter);

        switch (enter) {
            case 0:
                return popEnter1();
            case 1:
                return popEnter2();
            case 2:
                return popEnter3();
            case 3:
                return popEnter4();
            default:
                return null;
        }
    }

    private Animator getPopExit() {
        int enter = containerExitData.indexOf(textExitName.getText());
        System.out.println("================getPopExit》》》" + enter);
        switch (enter) {
            case 0:
                return popExit1();
            case 1:
                return popExit2();
            case 2:
                return popExit3();
            case 3:
                return popExit4();
            default:
                return null;
        }
    }

}
